package accs.model.amazon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import accs.model.AccountChecker;
import accs.model.Context;
import accs.model.ExtendedHttpClient;
import accs.model.utils.HeaderUtils;
import accs.model.utils.HtmlFormInputCollector;


public class AmazonChecker implements AccountChecker {

	private static final Pattern LOGINLINK_PATTERN=Pattern.compile("action=\"(.*)\"");
	
	
	public AmazonAccount login(String mail,String password, Context ctx) throws ClientProtocolException, IOException{
		
	DefaultHttpClient httpclient=new ExtendedHttpClient(ctx);
	
	HttpGet get=new HttpGet("https://www.amazon.de/ap/signin?_encoding=UTF8&openid.assoc_handle=deflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.de%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_signin");
	get.setHeader(HeaderUtils.getUserAgent());
	HttpResponse resp=httpclient.execute(get);
//	System.out.println(EntityUtils.toString(resp.getEntity()));
	BufferedReader br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent(),"utf-8"));
	String line=null;
	
	int linenumber = 0;
	String loginlink = "";
	while((line=br.readLine())!=null){
		line=line.trim(); //System.out.println(line);
		linenumber++;
		
		if(line.contains("ap_signin_form") && line.contains("http")){
			Matcher m=LOGINLINK_PATTERN.matcher(line);
			m.find();
			loginlink=m.group(1);
			System.out.println("Loginlink: "+loginlink);
			
		}
	}
	
	HttpGet get2=new HttpGet("https://www.amazon.de/ap/signin?_encoding=UTF8&openid.assoc_handle=deflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.de%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_signin");
	get2.setHeader(HeaderUtils.getUserAgent());
	HttpResponse resp3=httpclient.execute(get2);
	List<String> whitelist=new ArrayList<String>(Arrays.asList(new String[]{"appActionToken","openid.assoc_handle","openid.claimed_id","openid.identity","openid.mode","openid.ns","openid.ns.pape","openid.pape.max_auth_age","openid.return_to","pageId"}));
	List<NameValuePair> list=HtmlFormInputCollector.collectInputs(new BufferedReader(new InputStreamReader(resp3.getEntity().getContent(),"utf-8")),whitelist);

	for(NameValuePair bla : list) {
		System.out.println(bla.getName()+" -> "+bla.getValue());
	}
	
	HttpPost post=new HttpPost(loginlink);
	post.setHeader(HeaderUtils.getUserAgent());
	post.setHeader(HeaderUtils.getReferer("https://www.amazon.de/ap/signin?_encoding=UTF8&openid.assoc_handle=deflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.de%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dgno_signin"));
	
	list.add(new BasicNameValuePair("metadata1",""));
	list.add(new BasicNameValuePair("x","150"));
	list.add(new BasicNameValuePair("y","7"));
	list.add(new BasicNameValuePair("create","0"));
	list.add(new BasicNameValuePair("appAction","SIGNIN"));
	list.add(new BasicNameValuePair("email",mail));
	list.add(new BasicNameValuePair("password",password));
	post.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
	
	HttpResponse resp2=httpclient.execute(post);
//	String location=resp2.getFirstHeader("Location").getValue();
	EntityUtils.consume(resp2.getEntity());
//	System.out.println(EntityUtils.toString(resp2.getEntity()));
// 	System.out.println("---------------------------------------------------------------");
	
	
	HttpGet get3=new HttpGet("https://www.amazon.de/gp/yourstore/home?ie=UTF8&ref_=gno_signin&");
	get3.setHeader(HeaderUtils.getUserAgent());
	HttpResponse resp4=httpclient.execute(get3);
	System.out.println(EntityUtils.toString(resp4.getEntity()));
	
	return new AmazonAccount(mail,password);
	}

	public String getName() {
		return "amazon";
	}

	public URL getImage() {
		return null;
	}
}